<template>
  <div class="main_box">
    <div class="left_setting">
      <div class="top_box">
        <div class="photo">
          <a-avatar class="avatar" :size="40">
            <template #icon>
              <ClientOnly>
                <img :src="headPhoto" />
              </ClientOnly>
            </template>
          </a-avatar>
        </div>
        <div
          class="item"
          :class="{ active_item: useRoute().path == '/main/message' }"
        >
          <a-button type="link" @click="buttonClick('message')">
            <MessageFilled class="icon" />
            <div class="circle" v-if="messageCont">
              <ClientOnly>
                {{ messageCont }}
              </ClientOnly>
            </div>
          </a-button>
        </div>
        <div
          class="item"
          :class="{ active_item: useRoute().path == '/main/userList' }"
        >
          <a-button type="link" @click="buttonClick('userList')">
            <UserOutlined class="icon" />
            <div class="circle" v-if="userCont">
              <ClientOnly>
                {{ userCont }}
              </ClientOnly>
            </div>
          </a-button>
        </div>
        <div
          class="item"
          :class="{ active_item: useRoute().path == '/main/userUtils' }"
        >
          <a-button type="link" @click="buttonClick('userUtils')">
            <SettingFilled class="icon" />
          </a-button>
        </div>
      </div>
      <div class="bottom_box">
        <a-button type="link" @click="loginOut">
          <ImportOutlined class="icon" />
        </a-button>
      </div>
    </div>
    <div class="right_context">
      <slot></slot>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  UserOutlined,
  SettingFilled,
  ImportOutlined,
  MessageFilled,
  ExclamationCircleOutlined,
} from "@ant-design/icons-vue/lib/icons";
import { Modal } from "ant-design-vue";
import { createVNode } from "vue";
import { useUserInfo } from "~~/store/userInfo.store";
import {
  addUserApproveInterface,
  UserInterface,
  Message,
  MessageUser,
} from "~~/types";
import { useFriendList } from "~~/store/friendList.store";
import { useAddUserApprove } from "~~/store/addUserApproveList.store";
import { useMessageUserList } from "~~/store/messageUser.store";
import { useUserMessageList } from "~~/store/userMessage.store";
const { replace } = useRouter();

const buttonClick = (value: string) => {
  replace(`/main/${value}`);
};

let socket = null;

const loginOut = () => {
  Modal.confirm({
    content: "确定退出登录？",
    icon: createVNode(ExclamationCircleOutlined),
    async onOk() {
      return await new Promise((resolve, reject) => {
        useCookie("token").value = "";
        resolve(true);
        replace("/login");
      });
    },
    cancelText: "取消",
    okText: "确定",
  });
};

let userCont = computed(() => {
  let data = useAddUserApprove().getAddUserApproveList.filter(
    (item) => !item.state
  );
  return data.length;
});

let messageCont = computed(() => {
  let cont = useMessageUserList().getUserList.reduce(
    (pre: number, cur: MessageUser) => {
      return pre + cur.isWei;
    },
    0
  );
  return cont;
});

onMounted(() => {
  let messageUserListStore = useMessageUserList();
  let userMessageListStore = useUserMessageList();
  let friendList = useFriendList();
  let userId = useCookie("userId").value;
  let socket = initSocket();
  socket.emit("init", { userId: userId });
  socket.on("getUserApprove", (data: addUserApproveInterface[]) => {
    let addUserApprove = useAddUserApprove();
    addUserApprove.setAddUserApproveList(data);
  });

  socket.on("getMessageUserList", (data: any) => {
    messageUserListStore.setUserList(data);
  });

  socket.on("newUser", (data: UserInterface) => {
    friendList.setUserListItem(data);
  });
  socket.on("newUserApprove", (data: addUserApproveInterface) => {
    useAddUserApprove().setAddUserApproveListItem(data);
  });

  socket.on("sendNewMessage", (friend: UserInterface, message: Message) => {
    let index = messageUserListStore.getUserList.findIndex(
      (item: MessageUser) => item.user.id == friend.id
    );
    if (index == -1) {
      return messageUserListStore.setUserListItem({
        user: friend,
        isWei: 1,
        message: message,
      });
    }
    let newMessageUser = {
      user: friend,
      isWei: JSON.parse(
        JSON.stringify(messageUserListStore.getUserList[index].isWei)
      ),
      message: message,
    };
    messageUserListStore.messageUserList.splice(index, 1);
    if (userMessageListStore.selectUserId == friend.id) {
      newMessageUser.isWei = 0;
      userMessageListStore.setMessageListItem(message);
      messageUserListStore.setUserListItem(newMessageUser);
    } else {
      newMessageUser.isWei = newMessageUser.isWei + 1;
      messageUserListStore.setUserListItem(newMessageUser);
    }
  });
});

onUnmounted(() => (socket = null));

let headPhoto = computed(() => {
  return useUserInfo().userInfo?.headPhoto;
});
</script>

<style scoped lang="scss">
.main_box {
  width: 100%;
  height: 100%;
  display: flex;
  .left_setting {
    width: 60px;
    background-color: #35244e;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    align-items: center;

    .top_box {
      text-align: center;
      width: 100%;
      .photo {
        margin: 30px 0;
      }
      .item {
        width: 100%;
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        > .ant-btn {
          color: #695681;
          width: 100%;
          height: 100%;
          font-size: 20px;
          margin: 0;
          padding: 0;
        }
        .circle {
          position: absolute;
          background: #ff4d4f;
          height: 20px;
          min-width: 20px;
          font-size: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 10px;
          right: 5px;
          bottom: 5px;
          color: #fff;
        }
      }

      .active_item {
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        position: relative;
        background-color: #402e58;

        &::before {
          content: "";
          width: 5px;
          height: 100%;
          position: absolute;
          background-color: #845fa5;
          left: 0;
        }
        > .ant-btn {
          color: #fff;
          width: 100%;
          font-size: 20px;
          margin: 0;
          padding: 0;
        }
      }
    }
    .bottom_box {
      height: 80px;
      display: flex;
      align-items: center;

      > .ant-btn {
        color: #695681;
        font-size: 20px;
      }
    }
  }

  .right_context {
    width: calc(100% - 60px);
  }
}
</style>
